﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NF_Tools
{
    public partial class PageControl : UserControl
    {
        public event EventHandler onPageChange;
        Dictionary<int, string> kvDictonary = null;
        public PageControl()
        {
            InitializeComponent();
            BindCombox();
        }

        private int _pageIndex = 0;
        private int _totalPages = 0;
        private int _pageSize = 15;
        private int _totalCount = 0;

        /// <summary>
        /// 当前页面
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }
        /// <summary>
        /// 页面数量
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get
            {
                if (_totalCount != 0)
                    _totalPages = GetTotalPages();
                return _totalPages;
            }

        }
        /// <summary>
        /// 数据总量
        /// </summary>
        public int TotalCount
        {
            get { return _totalCount; }
            set { _totalCount = value; }
        }
        /// <summary>
        /// 获取总页数
        /// </summary>
        /// <returns></returns>
        private int GetTotalPages()
        {
            if (PageSize == 0)
                return PageSize;
            int totalPages = TotalCount / PageSize;
            if (TotalCount % PageSize == 0)
                return totalPages;
            else
                return totalPages + 1;
        }
        /// <summary>
        /// 设置所有的控件可用
        /// </summary>
        private void SetAllControlsEnable()
        {
            btnFirstPage.Enabled = true;
            btnPrePage.Enabled = true;
            btnNextPage.Enabled = true;
            btnLastPage.Enabled = true;
           // Golinklbl.Enabled = true;
        }

        private void BindCombox()
        {
            //kvDictonary = new Dictionary<int, string>();
            //kvDictonary.Add(1, "15");
            //kvDictonary.Add(2, "30");
            //kvDictonary.Add(3, "45");
            //kvDictonary.Add(4, "60");
            //BindingSource bs = new BindingSource();
            //bs.DataSource = kvDictonary;
            //PageCountCob.DataSource = bs;
            //PageCountCob.ValueMember = "Key";
            //PageCountCob.DisplayMember = "Value";
            //PageCountCob.SelectedIndex = 0;
        }
        /// <summary>
        /// 对外暴漏的方法
        /// </summary>
        /// <param name="totalcount"></param>
        public void SetPages(int totalcount)
        {
            _totalCount = totalcount;
            SetPages(false);
        }
        private void SetPages(bool callevent)
        {
            //设置当前页
            CurrentPageIndex.Text = PageIndex + "/" + TotalPages;
            TotalLabel.Text = "共"+ TotalCount + "条";
            if (callevent && onPageChange != null)
                onPageChange(this, null);
            SetAllControlsEnable();
            //如果只有一页
            if (TotalPages == 1)
            {
                btnFirstPage.Enabled = false;
                btnNextPage.Enabled = false;
                btnLastPage.Enabled = false;
                btnPrePage.Enabled = false;
                //Golinklbl.Enabled = false;
            }
            //第一页
             else if (PageIndex == 1)
            {
                btnFirstPage.Enabled = false;
                btnPrePage.Enabled = false;
            }
            //最后一页
            else if (PageIndex == TotalPages)
            {
                btnNextPage.Enabled = false;
                btnLastPage.Enabled = false;
            }
            if (TotalPages == 0)
            {
                btnNextPage.Enabled = false;
                btnLastPage.Enabled = false;
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            SetPages(true);
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrePage_Click(object sender, EventArgs e)
        {
            PageIndex = Math.Max(1, PageIndex - 1);
            SetPages(true);
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNextPage_Click(object sender, EventArgs e)
        {
            PageIndex = Math.Min(TotalPages, PageIndex + 1);
            SetPages(true);
        }

        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLastPage_Click(object sender, EventArgs e)
        {
            PageIndex = TotalPages;
            SetPages(true);
        }


      
        bool isTextChanged = false;
        
        private void PageControl_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.Silver);
            Graphics g = e.Graphics;
            g.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
            p.Dispose();

        }
    }
}
